package com.bodystm.hardwarecavas;

public class Resampling {

    private double Ratio, Progress, Pace, Adjust;
    private int status;
    private double Seg[] = new double[2];
    private double Remain;
    private int nInitRemain;
    private double Step, diff, count;
    public void Resampling() {
        Ratio = 0;
        Progress = 0;
        Pace = 0;
        Adjust = 0;
        status = 0;
        status = 0;
        Seg[0] = 0;
        Seg[1] = 0;
        Remain = 0;
        nInitRemain = 0;
        Step = 0;
        diff = 0;
        count = 0;
    }

    /**
     * 重采样算法
     * @param dstF 目标采样率,单位Hz
     * @param srcF 原始采样率,单位Hz
     * @param Data 数据点
     * @return
     *          0:  使用该点
     *          -1: 跳过该点
     *          >0: 需要添加的点数
     *          -2: 参数错误
     */
    public int Resampling(float dstF, float srcF, double Data) {
        if (dstF <= 0 || srcF <= 0) {
            return -2;
        }
        double slop;
        int i;

        if (nInitRemain == 0)
        {
            Remain = dstF;
            nInitRemain = 1;
        }

        if (status < 2) //initialization
        {
            Seg[0] = Seg[1];
            Seg[1] = Data;
            status++;
            if (status != 2) //make sure that at least two values has been added to the Segment
            {
                //            *ResValue = Data; //always return the first value while the program is initializing
                count = dstF;
                return 0;
            }
            Ratio = srcF / dstF;
        }
        else
        {
            Seg[0] = Seg[1];
            Seg[1] = Data;
            Ratio = (srcF) / Remain;
        }

        if (Ratio < 1) //increase sampling rate
        {
            Step = 1 / Ratio;
            slop = (Seg[1] - Seg[0]) / dstF; //slop between the two data points
            //        for (i = 0; i < (int)(Step); i++)
            //        {
            //            *(ResValue + i) = ((i + 1)*srcF - diff)*slop + Seg[0];
            //        }
            diff = (Step - (int)(Step))*srcF;
            Remain = diff + dstF; //remaining time bewteen next sampling point
            return (int)(Step) - 1;
        }
        else if (Ratio > 1)	//lowering sampling rate
        {
            Progress = count / (srcF);
            if (Progress < 1) //not reaching resampling point
            {
                count += dstF;
                return -1;
            }
            else
            {
                diff = dstF - (Progress - (int)(Progress))*srcF;
                slop = (Seg[1] - Seg[0]) / dstF;
                //            *ResValue = diff*slop + Seg[0];
                //Adjust = dstF - diff;
                count = dstF - diff + dstF;
                return 0;
            }
        }
        else
        {
            //        *ResValue = Data;
            Adjust = 0;
            Remain = dstF;
            return 0;
        }
    }

    public void main() {
        int nLoop = 20;
        for (int i = 0; i < nLoop; i++) {
            int nRet = Resampling(100, 50, i);
            for (int j = 0; j <= nRet; j++) {
                System.out.println("nRet is " + nRet + ", val is " + i);
            }
        }
    }
}
